package com.glsc.ngateway.opmanage.service.deposit;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryBank;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryBaseDataQuarter;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryCounterBankQuarter;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.deposit.DepositoryCounterBankQuarterRepository;
import com.glsc.ngateway.opmanage.dto.DepositoryCounterBankExcelDto;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import com.glsc.ngateway.opmanage.utils.report.ReportFileConfigEnum;
import com.glsc.ngateway.opmanage.utils.report.ReportFileUtil;
import com.glsc.ngateway.opmanage.utils.report.ReportUploadContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
public class DepositoryCounterBankQuarterService {


    @Resource
    private DepositoryCounterBankQuarterRepository baseDataQuarterRepository;

    @Resource
    private DepositoryBankService bankService;

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<DepositoryCounterBankQuarter> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<DepositoryCounterBankQuarter> page = baseDataQuarterRepository.findAll(SpecificationUtil.buildSpecification(param, DepositoryCounterBankQuarter.class), pageable);

        return page;
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public DepositoryCounterBankQuarter findById(Integer id) {
        Optional<DepositoryCounterBankQuarter> optional = baseDataQuarterRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public DepositoryCounterBankQuarter save(DepositoryCounterBankQuarter obj) {
        //上季度客户结息利率=上季度客户结息金额/上季度柜台余额均值
        obj.setCustomerInterestRate(obj.getCustomerInterestAmount().divide(obj.getCounterBalanceAverage(), 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));

        //上季度银行平均利率=上季度银行结息金额/上季度银行余额均值
        obj.setAverageBankRate(obj.getBankInterestAmount().divide(obj.getAverageBankBalance(), 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));
        obj.setDeleted("N");
        return baseDataQuarterRepository.save(obj);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void delete(Integer id) {
        baseDataQuarterRepository.deleteById(id);
    }


    @Transactional
    public ReportUploadContext<DepositoryCounterBankExcelDto, String> important(MultipartFile file) throws IOException {
        ReportUploadContext<DepositoryCounterBankExcelDto, String> context = ReportUploadContext.<DepositoryCounterBankExcelDto, String>builder().config(ReportFileConfigEnum.DEPOSITORY_COUNTER_BANK_QUARTER).file(file).build();

        ReportFileUtil.readSingleExcel(context);
        Assert.notEmpty(context.getReadRes(), "上传内容不可为空");


        List<DepositoryCounterBankExcelDto> readResList = context.getReadRes();
        if (CollectionUtils.isEmpty(readResList)) {
            return context;
        }

        for (DepositoryCounterBankExcelDto dto : readResList) {
            DepositoryCounterBankQuarter counterBankQuarter = DepositoryCounterBankQuarter.builder().effectBeginDate(dto.getEffectBeginDate()).effectEndDate(dto.getEffectEndDate()).counterBalanceAverage(dto.getCounterBalanceAverage())
                    .customerInterestRate(dto.getCustomerInterestRate()).averageBankBalance(dto.getAverageBankBalance()).bankInterestAmount(dto.getBankInterestAmount())
                    .averageBankRate(dto.getAverageBankRate()).customerInterestAmount(dto.getCustomerInterestAmount()).build();

            save(counterBankQuarter);
        }


        return context;
    }


}
